package org.news.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.aliyuncs.exceptions.ClientException;
import com.dtflys.forest.annotation.Get;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.news.web.constant.CategoriesStatus;
import org.news.web.constant.NewsConstant;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.NewsVo;
import org.news.web.domain.bo.NewsBo;
import org.news.web.service.INewsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 新闻功能
 *
 * @author Gu Ci
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/news")
public class NewsController extends BaseController {

    private final INewsService newsService;

    /**
     * 查询新闻功能列表
     */
    @SaCheckPermission("web:news:list")
    @GetMapping("/list")
    public TableDataInfo<NewsVo> list(NewsBo bo, PageQuery pageQuery) {
        return newsService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询发布的新闻功能
     */
    @GetMapping("/FBlist")
    public R<List<NewsVo>> FBlist(String Key){
       return R.ok(newsService.FBlist(Key));
    }

    /**
     * 导出新闻功能列表
     */
    @SaCheckPermission("web:news:export")
    @Log(title = "新闻功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NewsBo bo, HttpServletResponse response) {
        List<NewsVo> list = newsService.queryList(bo);
        ExcelUtil.exportExcel(list, "新闻功能", NewsVo.class, response);
    }

    /**
     * 获取新闻功能详细信息
     *
     * @param newsId 主键
     */
    @SaCheckPermission("web:news:query")
    @GetMapping("/{newsId}")
    public R<NewsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long newsId) {
        return R.ok(newsService.queryById(newsId));
    }

    /**
     * 新增新闻功能
     */
    @SaCheckPermission("web:news:add")
    @Log(title = "新闻功能", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NewsBo bo) throws UnsupportedEncodingException, ClientException {
        return toAjax(newsService.insertByBo(bo));
    }

    /**
     * 修改新闻功能
     */
    @SaCheckPermission("web:news:edit")
    @Log(title = "新闻功能", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NewsBo bo) throws UnsupportedEncodingException, ClientException {
        return toAjax(newsService.updateByBo(bo));
    }
    /**
     * 修改新闻审核信息
     */
    @PostMapping("/updateStatus")
    public R<Void> updateStatus(@RequestBody NewsBo bo) throws UnsupportedEncodingException, ClientException {
        return toAjax(newsService.updateStatus(bo));
    }

    /**
     * 删除新闻功能
     *
     * @param newsIds 主键串
     */
    @SaCheckPermission("web:news:remove")
    @Log(title = "新闻功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] newsIds) {
        return toAjax(newsService.deleteWithValidByIds(List.of(newsIds), true));
    }

    /**
     * 获取来源类型
     */
    @GetMapping("/getLaiYuan")
    public R<List<CategoriesStatus>> getLaiYuan(){
        return R.ok(NewsConstant.NEWS_LAI_YUAN);
    }

    /**
     * 获取新闻审核类型
     */
    @GetMapping("/getShenHe")
    public R<List<CategoriesStatus>> getShenHe(){
        return R.ok(NewsConstant.NEWS_SHEN_HE);
    }
}
